package cn.yuan.order.controller;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.basic.util.LoginContext;
import cn.yuan.basic.util.PageList;
import cn.yuan.order.domain.AdoptOrder;
import cn.yuan.order.query.AdoptOrderQuery;
import cn.yuan.order.service.IAdoptOrderService;
import cn.yuan.user.domain.Logininfo;
import cn.yuan.util.AjaxResult.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            System.out.println(loginInfo);
            //为支付准备的回调
            String payData = adoptOrderService.submit(params,loginInfo);
            return AjaxResult.me().setResultObj(payData);
        }catch(BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误");
        }

    }
    //1.平台管理员、店铺 shopId
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.admin(query,loginInfo.getId());
    }
    //2.前台用户的 自己的 userId
    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.user(query,loginInfo.getId());
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "领养订单详情接口",notes = "包括关联属性")
    public AdoptOrder info(@PathVariable("id") Long id){
        return adoptOrderService.loadById(id);
    }
}
